package cn.edu.lingnan.translation.controller;

import cn.edu.lingnan.translation.entity.Question;
import cn.edu.lingnan.translation.entity.vo.QuestionQueryVo;
import cn.edu.lingnan.translation.service.QuestionService;
import cn.edu.lingnan.translation.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    @ApiOperation(value = "获取题库分页列表")
    @PostMapping("/{page}/{limit}")
    public R getQuestionPage (
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "QuestionQueryVo", value = "查询对象", required = false)
            @RequestBody QuestionQueryVo questionQueryVo){
        Page<Question> questionPage = new Page<>(page,limit);
        //条件
        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        String status = questionQueryVo.getStatus().toString();
        if (!StringUtils.isEmpty(status)){
            wrapper.like("status", questionQueryVo.getStatus());
        }
        if (!StringUtils.isEmpty(questionQueryVo.getTitle())){
            wrapper.like("title", questionQueryVo.getTitle());
        }
        if (!StringUtils.isEmpty(questionQueryVo.getType())){
            wrapper.like("type", questionQueryVo.getType());
        }
        if (!StringUtils.isEmpty(questionQueryVo.getTheme())){
            wrapper.like("theme", questionQueryVo.getTheme());
        }
        IPage<Question> iPage = questionService.getQuestions(questionPage,wrapper);
        return R.ok().data("question", iPage.getRecords()).data("total", iPage.getTotal());
    }

    @ApiOperation(value = "题库更新")
    @PostMapping("/save")
    public R updateQuestion(@RequestBody Question question){
        List<String> id = new ArrayList<>();
        id.add(question.getId());
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
        question.setUpdateTime(dateTime.format(formatter));
        questionService.updateQuestions(id, question);
        return R.ok();
    }

    @ApiOperation(value = "状态更新:启动")
    @PostMapping("/save1")
    @ResponseBody
    public R updateStatusQuestion1(@RequestParam("ids") List<String> ids){
        Question question = new Question();
        question.setStatus(true);
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
        question.setUpdateTime(dateTime.format(formatter));
        questionService.updateQuestions(ids, question);
        return R.ok();
    }

    @ApiOperation(value = "状态更新:停用")
    @PostMapping("/save0")
    @ResponseBody
    public R updateStatusQuestion0(@RequestParam("ids") List<String> ids){
        Question question = new Question();
        question.setStatus(false);
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
        question.setUpdateTime(dateTime.format(formatter));
        questionService.updateQuestions(ids, question);
        return R.ok();
    }

    @ApiOperation(value = "删除试题")
    @PostMapping("/delete")
    @ResponseBody
    public R deleteQuestion(@RequestParam("ids") List<String> ids){
        questionService.deleteQuestions(ids);
        return R.ok();
    }

    @ApiOperation(value = "添加题库")
    @PostMapping("/insert")
    public R insertQuestion(@RequestBody Question question){
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
        question.setCreateTime(dateTime.format(formatter));
        question.setIsDelete(false);
        //设默认状态为开启
        question.setStatus(true);
        questionService.insertQuestions(question);
        return R.ok();
    }
}
